package com.shycloud.mido.homework.thread;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.homework.api.entity.WeekMain;
import com.shycloud.mido.homework.api.vo.WeekPayloadVO;
import com.shycloud.mido.homework.constant.Constant;
import com.shycloud.mido.homework.mapper.WeekMainMapper;
import com.shycloud.mido.homework.service.MidoHomeworkWeekService;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class WeekAsyncTask {

	/** 周报服务 */
	@Autowired
	private MidoHomeworkWeekService midoHomeworkWeekService;
	@Autowired
	private WeekMainMapper weekMainMapper;

	@Async
	public void asyncExecute(LocalDateTime now) throws Exception {
		StopWatch stopWatchMain = new StopWatch();
		stopWatchMain.start();
		if (Objects.isNull(now)) {
			now = LocalDateTime.now();
		}
		WeekPayloadVO payload = new WeekPayloadVO();
		payload.setNow(now);
		try {
			log.info("weekJobBuild周报执行时间: {} ", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now));

			//构建周报数据
			log.info("weekJobBuild start: {}", payload);
			//构建周报
			midoHomeworkWeekService.buildWeekMain(payload);
			//更新状态
			if (StringUtils.isNotBlank(payload.getWeekMainId())) {
				LambdaUpdateWrapper<WeekMain> weekMailUW = Wrappers.<WeekMain>lambdaUpdate()
						.set(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCING)
						.set(WeekMain::getGenStarttime, LocalDateTime.now())
						.eq(WeekMain::getId, payload.getWeekMainId());
				weekMainMapper.update(null, weekMailUW);
			}
			log.info("weekJobBuild end: {}", payload);

			//========================== 解析作业数据 START ================================//
			log.info("resolveHomeworkData start: {}", payload);
			midoHomeworkWeekService.resolveHomeworkData(payload);
			log.info("resolveHomeworkData end: {}", payload);
			//========================== 解析作业数据 E N D ================================//

			//========================== 生成周报数据 START ================================//
			log.info("generateWeekMember start: {}", payload);
			midoHomeworkWeekService.generateWeekMember(payload);
			log.info("generateWeekMember end: {}", payload);
			//========================== 生成周报数据 E N D ================================//

			//更新步骤
			if (StringUtils.isNotBlank(payload.getWeekMainId())) {
				LambdaUpdateWrapper<WeekMain> weekMailUW = Wrappers.<WeekMain>lambdaUpdate()
						//预处理完成
						.set(WeekMain::getStep, Constant.WEEK_MAIN_STEP_EXEC_DONE)
						//状态更新 - 已完成
						.set(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCSUCCESS)
						//更新时间 - 周报生成的结束时间
						.set(WeekMain::getGenEndtime, LocalDateTime.now())
						//周报ID
						.eq(WeekMain::getId, payload.getWeekMainId());
				weekMainMapper.update(null, weekMailUW);
				payload.getWeekMain().setStep(Constant.WEEK_MAIN_STEP_EXEC_DONE);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			e.printStackTrace();
			//更新步骤
			if (StringUtils.isNotBlank(payload.getWeekMainId())) {
				LambdaUpdateWrapper<WeekMain> weekMailUW = Wrappers.<WeekMain>lambdaUpdate()
						//处理失败
						.set(WeekMain::getStep, Constant.WEEK_MAIN_STEP_FAIL)
						.set(WeekMain::getStatus, Constant.WEEK_MAIN_STATUS_CALCERROR)
						.set(WeekMain::getGenEndtime, LocalDateTime.now())
						.eq(WeekMain::getId, payload.getWeekMainId());
				weekMainMapper.update(null, weekMailUW);
			}
		}
		stopWatchMain.stop();
		log.info("asyncExecute耗时：{}", (double) stopWatchMain.getTime() / 1000 + "s");
	}

}